//
//  STSearchVC.m
//  YiYueStore
//
//  Created by zhouzhouzhou on 2018/10/22.
//  Copyright © 2018年 YuYiLian. All rights reserved.
//

#import "STSearchVC.h"
#import "YLlNavXUIView.h"
#import "CustomLeftTextField.h"
#import "STHairerInfoVC.h"

#import "STDoorInfoVC.h"


@interface STSearchVC ()<UITextFieldDelegate,STHairHouseListViewDelegate>{
    CustomLeftTextField *searchHair;
    NSMutableArray * arrModels;
    STHairHouseListView * houseListView;
    STHairBriefInfoModel * hairerModel;
}

@end

@implementation STSearchVC

- (void)viewDidLoad {
    [super viewDidLoad];
     [self wr_setNavBarTintColor:[UIColor blackColor]];
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 60, 40)];
    [button addTarget:self action:@selector(searchHairerAction) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"搜索" forState:UIControlStateNormal];
    [button setTitleColor:BlackColor forState:UIControlStateNormal];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = item;
    
    YLlNavXUIView *centerView = [[YLlNavXUIView alloc]initWithFrame:CGRectMake(0.0, 0.0, SCREEN_WIDTH - 120, 30.0)];
    self.navigationItem.titleView = centerView;
    searchHair = [[CustomLeftTextField alloc]init];
    [centerView addSubview:searchHair];
    searchHair.backgroundColor = BgColor;
    searchHair.delegate = self;
    searchHair.placeholder = @"请输入门店名";
    searchHair.leftImageView.image = [UIImage imageNamed:@"searchIcon"];
    [searchHair becomeFirstResponder];
    searchHair.layer.cornerRadius = 15.0f;
    searchHair.layer.masksToBounds = YES;
    [searchHair mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(5);
        make.bottom.mas_equalTo(-5);
    }];

    [searchHair addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
    
    [self loadSerachListView];
    
}
#pragma mark - UITextField Delegate
-(void)textFieldChanged:(UITextField *)textField
{
//    DLog(@"textField.text==%@",textField.text);
    // FIXME: 测试 限制输入限制
    if (textField.text.length > 50) {
        textField.text = [textField.text substringToIndex:50];
    }
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if ([string isEqualToString:@"\n"]){
        [textField resignFirstResponder];
        [self searchHairerAction];
        return NO;
    }
    return YES;
}

- (void)searchHairerAction {

    [searchHair resignFirstResponder];
    [STHairService searchStoreWithStoreStr:searchHair.text completion:^(id result, NSError *error) {
        if (result) {
            NSArray *array = [NSArray modelArrayWithClass:STHairHouseListModel.class json:result];
            arrModels = (NSMutableArray *)array;
            // FIXME: ???搜索的结果应该不用区分是否签约
            [houseListView setHairHouseListView:1 data:array];
        }
    }];
    
}

-(void)loadSerachListView{
    houseListView = [[STHairHouseListView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    houseListView.delegate = self;
    [houseListView setHairHouseListView:0 data:@[]];
    [self.view addSubview:houseListView];
}

-(void)hairHouseListSelNum:(NSInteger)selNum;{
    STHairHouseListModel *model = arrModels[selNum];
    
    STDoorInfoVC * doorInfoVC  =[STDoorInfoVC new];
    doorInfoVC.isSign = NO;
    doorInfoVC.stylistId = model.storeId;
    [self.navigationController pushViewController:doorInfoVC animated:YES];
}

@end
